package com.code.day02.service;

import com.code.day02.dao.AccountDao;
import com.code.day02.utils.ConnectionManager;
import com.code.day02.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountService {
    public void transfer(String outname, String inname, int money){
        AccountDao accountDao = new AccountDao();
        try {
            ConnectionManager.begin();
            accountDao.outMoney(outname, money);
            accountDao.inMoney(inname, money);
            //System.out.println(1/0);
            System.out.println("转账成功");
            ConnectionManager.commit();
        }catch (Exception e){
            try{
                ConnectionManager.rollback();
            }catch (SQLException ex){
                ex.printStackTrace();
            }
            System.out.println("转账失败");
            e.printStackTrace();
        }finally {
            ConnectionManager.close();
        }

    }
}
